package com.forfunnet.minjian.ui;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.forfunnet.minjian.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * Created by RexQian on 4/3/16.
 */
@EActivity(R.layout.activity_main)
public class MainActivity extends BaseFragmentActivity {

    FragmentManager mFragmentManager;
    Fragment mFragmentCur;
    /** 主页 */
    Fragment mFragmentHome = new HomeFragment_();

    Fragment mFragmentFind = new FindFragment();

    @ViewById(R.id.rg_tab)
    RadioGroup mRadioGroup;


    int mUserId;
    @AfterViews
    void init() {
        mUserId = mApp.pref().userId().get();

        mFragmentManager = getSupportFragmentManager();
        mFragmentCur = mFragmentHome;
        mFragmentManager.beginTransaction().add(R.id.frame_container, mFragmentHome).commit();

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                Fragment to = getFragmentByCheckId(checkedId);
                switchFragment(mFragmentCur, to);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }


    Fragment getFragmentByCheckId(int checkId) {
        switch(checkId) {
            case R.id.rb_home:
                return mFragmentHome;
            case R.id.rb_find:
                return mFragmentFind;
            case R.id.rb_mine:
                return null;
            default:
                return null;
        }
    }

    void switchFragment(Fragment from, Fragment to) {
        if (mFragmentCur != to) {
            mFragmentCur = to;
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            if (!to.isAdded()) {
                transaction.hide(from).add(R.id.frame_container, to).commit();
            }
            else {
                transaction.hide(from).show(to).commit();
            }
        }
    }
}
